package org.jash.demo2205.shop

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.demo2205.entity.BannerEntity
import org.jash.demo2205.entity.Category
import org.jash.demo2205.entity.Goods
import org.jash.demo2205.mvvm.BaseViewModel

class CategoryViewModel:BaseViewModel() {
    val categoriesLiveData by lazy { MutableLiveData<List<Category>>() }
    val goodsLiveData by lazy { MutableLiveData<List<Goods>>() }

    fun getCategories() {
        viewModelScope.launch {
            try {
                val res = service.getAllCategory()
                if (res.code == 200) {
                    categoriesLiveData.postValue(res.data)
                } else {
                    throw RuntimeException(res.message)
                }
            } catch (e:Exception) {
                failureLiveData.postValue(e)
            }
        }
    }
    fun getGoods(cid:Int, page:Int, size:Int) {
        viewModelScope.launch {
            try {
                val res = service.getGoods(cid, page, size)
                if (res.code == 200) {
                    goodsLiveData.postValue(res.data)
                } else {
                    throw RuntimeException(res.message)
                }
            } catch (e:Exception) {
                failureLiveData.postValue(e)
            }
        }

    }
}